--[[
refer: <<program in lua>> chapter 9

GET /TR/html401/html40.txt HTTP/1.1
host: www.w3c.org

problem: file size not right
--]]

local socket = require('socket')
host = "www.w3.org"
-- host = "localhost"

local port = 80
if host=='localhost' then port=8888 end

function down(host, file)
  local c = assert(socket.connect(host, port), "error at socket.connect()")
  local str = "GET "..file.." HTTP/1.1\r\nHost: " .. host .. "\r\n\r\n"

  local count=0
  c:send(str)

  while true do
    local s, status, f = receive(c)
    
    -- print('status=', status)
    -- if s then print('#s=',#s) end

    if s then
      count = count + #s
      -- print('get size=',#s)
    elseif f then
      count = count + #f
    end

    if status == "closed" then break end
  end

  c:close()
  print(file,'count=' .. count)
end

function receive(connection)
  -- print("exec receive")
  connection:settimeout(0)
  local s, status, f = connection:receive(2^12)  -- f= fragment
  -- local s, status = connection:receive('*a')

  -- print('receive status=',status)
  -- if s then print('#s=',#s) end
  -- if f then print('#f=',#f) end

  if status == 'timeout' then
    coroutine.yield(connection) 
  end
  return s, status, f
end

local threads = {}

function get(host, file)
  local co = coroutine.create(function() down(host, file)  end )
  table.insert(threads, co)
end

function dispatcher ()
  while true do
    local n = table.getn(threads)
    -- print('threads count=',n)
    if n == 0 then break end   -- no more threads to run
    local connections = {}
    for i=1,n do
      local status, res = coroutine.resume(threads[i])
      -- print(i,costatus,res) -- res = tcp connection
      if not res then       -- thread finished its task?
        -- print("remove threads",i)
        table.remove(threads, i)
        break
      else    -- timeout
        -- print('insert connections')
        table.insert(connections, res)
      end
    end

    if table.getn(connections) == n then
      -- print('wait connections read')
      socket.select(connections)
    end
  end
end

  -- host = "www.baidu.com"
  -- get(host, "/")
  local startAt = socket.gettime()
  local cpuTime1 = os.clock()

  host = "www.w3.org"
  -- host = "localhost"
  get(host, "/TR/REC-html32.html")
  get(host, "/TR/html401/html40.txt")
  get(host, "/TR/2002/REC-xhtml1-20020801/xhtml1.pdf")
  get(host, "/TR/2000/REC-DOM-Level-2-Core-20001113/DOM2-Core.txt")


  dispatcher()
  print("time elapse = ",socket.gettime() - startAt)
  print("cpu time elapse = ",os.clock() - cpuTime1)

-- time elapse =   13.41762804985
-- cpu time elapse =   0.207382
-- vs block io
-- time elapse =   28.581546783447
-- cpu time elapse =   0.088438